from dashscope import Application
from configs.other_config import DASHSCOPE_API_KEY, GENERATE_LAWYER_REPORT_APPID, GENERATE_EMOTION_REPORT_APPID
from configs.logging_config import configure_logging
from fastapi import FastAPI, APIRouter
from server.utils import BaseResponse
from loguru import logger
import os
import uvicorn
import json
import datetime

configure_logging()

router = APIRouter()

@router.get("/bailian/v1/generate_lawyer_report")
def generate_lawyer_report():
    elapsed = 0
    try:
        app_id = GENERATE_LAWYER_REPORT_APPID
        query = "请生成一份律师每日新闻简报" # 随便写，没用，只是用来满足输入参数
        call_params = {
            "api_key": os.getenv("DASHSCOPE_API_KEY", DASHSCOPE_API_KEY),
            "app_id": app_id,
            "prompt": query,
            "stream": False,
        }
        start_time = datetime.datetime.now()
        responses = Application.call(**call_params)
        end_time = datetime.datetime.now()
        elapsed = (end_time - start_time).total_seconds()
        if responses.status_code == 200:
            logger.success(f"律师每日报告生成成功 | 耗时: {elapsed:.2f}s")
            # 报告内容
            report_content = json.loads(responses.output.text)["content"]
            logger.info(report_content)
            return BaseResponse(
                code=responses.status_code,
                msg=f"律师每日报告生成成功 | 耗时: {elapsed:.2f}s",
                data=report_content
            )
        else:
            logger.error(f"律师每日报告生成失败 | 耗时: {elapsed:.2f}s | 错误: {responses.message}")
            return BaseResponse(
                code=responses.status_code,
                msg=f"律师每日报告生成失败 | 耗时: {elapsed:.2f}s | 错误: {responses.message}",
                data=None
            )
    except Exception as e:
        logger.error(f"生成律师每日报告失败: {e} | 耗时: {elapsed:.2f}s")
        return BaseResponse(
            code=500,
            msg=f"生成律师每日报告失败: {e} | 耗时: {elapsed:.2f}s",
            data=None
        )

@router.get("/bailian/v1/generate_emotion_report")
def generate_emotion_report():
    elapsed = 0
    try:
        app_id = GENERATE_EMOTION_REPORT_APPID
        query = "请生成一份情感专家每日新闻简报" # 随便写，没用，只是用来满足输入参数
        call_params = {
            "api_key": os.getenv("DASHSCOPE_API_KEY", DASHSCOPE_API_KEY),
            "app_id": app_id,
            "prompt": query,
            "stream": False,
        }
        start_time = datetime.datetime.now()
        responses = Application.call(**call_params)
        end_time = datetime.datetime.now()
        elapsed = (end_time - start_time).total_seconds()
        if responses.status_code == 200:
            logger.success(f"情感专家每日报告生成成功 | 耗时: {elapsed:.2f}s")
            # 报告内容
            report_content = json.loads(responses.output.text)["content"]
            logger.info(report_content)
            return BaseResponse(
                code=responses.status_code,
                msg=f"情感专家每日报告生成成功 | 耗时: {elapsed:.2f}s",
                data=report_content
            )
        else:
            logger.error(f"情感专家每日报告生成失败 | 耗时: {elapsed:.2f}s | 错误: {responses.message}")
            return BaseResponse(
                code=responses.status_code,
                msg=f"情感专家每日报告生成失败 | 耗时: {elapsed:.2f}s | 错误: {responses.message}",
                data=None
            )
    except Exception as e:
        logger.error(f"生成情感专家每日报告失败: {e} | 耗时: {elapsed:.2f}s")
        return BaseResponse(
            code=500,
            msg=f"生成情感专家每日报告失败: {e} | 耗时: {elapsed:.2f}s",
            data=None
        )

if __name__ == "__main__":
    app = FastAPI()
    app.include_router(router)
    uvicorn.run(app, host="0.0.0.0", port=8000)